package com.yipingnai.blog.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: 一瓶奶
 * @Url: www.yipingnai.cn
 * @Date: 2020/6/20 10:50
 */
@Component
public class RedisUtils {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    // 删除key
    public Boolean delKey(String key){
        return redisTemplate.delete(key);
    }

    // 是否存在key
    public boolean isExitKey(String key){
        return redisTemplate.hasKey(key);
    }

    // 根据键获取所有的field value
    public Map getFieldValueByKey(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    // hash中是否存在某字段
    public boolean isExitField(String key,String field){
        return redisTemplate.opsForHash().hasKey(key,field);
    }
    // 添加hash的field字段，不存在才添加
    public boolean setField(String key,String field){
        return redisTemplate.opsForHash().putIfAbsent(key,field,1);
    }

    // 字段自增1
    public Long updateFieldValue(String key, String field, int i){
        return redisTemplate.opsForHash().increment(key,field,i);
    }
}
